package com.topvision.common.utils;

import com.topvision.common.response.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;

/**
 * @author shengwm
 * @version V0.0.1
 * @Title: Responses
 * @Package com.topvision.common.utils
 * @Description: TODO
 * @date 2017/5/23
 */
public class Responses {
    public static ResponseEntity error(String error, String message) {
        return new ResponseEntity(ErrorResponse.of(error, message), HttpStatus.OK);
    }

    public static ResponseEntity validate(BindingResult result) {
        // 校验没有通过
        if (result.hasErrors()) {
            return error("field_" + result.getFieldError().getField(), result.getFieldError().getDefaultMessage());
        }
        return null;
    }
}
